#THIS FILE IS FOR THE VIEWS OF CLASS DISPLAY#


# Create your views here.
from myuwaterloo.harddata.models import *
from myuwaterloo.rateclasses.models import *
from myuwaterloo.classdisplay.forms import RatingForm
from myuwaterloo.ratemyprof.models import *

#from models import sessionTable
from django.db.models import Q
from django import forms as forms
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
#from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.forms import ModelForm


def classdisplay(request, classdep, classid):
	
	user = request.user
	
	userRating = 0
	
	forceUpdate = 0
	
	rating = 0
	ratingcount=0
	ratingid=0
	
	difficult = 0
	heavyworkload = 0
	gladtook = 0
	interesting = 0
	extracount=0
	
	userDifficult = 0
	userInteresting = 0
	userGladtook = 0
	userHeavyworkload = 0
	
	prof = []
	profrating = 0

	
	
	
	classinfo = allcourses.objects.filter(department=classdep, class_field=classid)
	classtitle = classinfo[0].department +' '+classinfo[0].class_field+' - '+classinfo[0].name
	courseidentifier = classdep+classid
	
	winter09info = winter09.objects.filter(department=classdep, class_field=classid)
	spring09info = spring09.objects.filter(department=classdep, class_field=classid)
	fall09info = fall09.objects.filter(department=classdep, class_field=classid)
	
	usercoursedata = userclasstable.objects.filter(courseidentifier=courseidentifier)
	
	
	# SUPER UGLY CODE TO REDO LATER#
	for result in winter09info:
		prof = result.instructor.split(" ")
		ratemyprofData = ratemyprof.objects.filter(name = prof[0])
		
		if ratemyprofData:
			result.profRating = ratemyprofData[0].rating
			result.profLink = ratemyprofData[0].link
		
		if result.section.startswith("TUT"): 
			result.section = "Tutorial" + result.section[3:7]
			
		if result.section.startswith("LEC"):
			result.section = "Lecture" + result.section[3:7]
			
	for result in spring09info:
		prof = result.instructor.split(" ")
		ratemyprofData = ratemyprof.objects.filter(name = prof[0])
		
		if ratemyprofData:
			result.profRating = ratemyprofData[0].rating
			result.profLink = ratemyprofData[0].link
		
		if result.section.startswith("TUT"): 
			result.section = "Tutorial" + result.section[3:7]
			
		if result.section.startswith("LEC"):
			result.section = "Lecture" + result.section[3:7]
			
	for result in fall09info:
		prof = result.instructor.split(" ")
		ratemyprofData = ratemyprof.objects.filter(name = prof[0])
		
		if ratemyprofData:
			result.profRating = ratemyprofData[0].rating
			result.profLink = ratemyprofData[0].link
		
		if result.section.startswith("TUT"): 
			result.section = "Tutorial" + result.section[3:7]
			
		if result.section.startswith("LEC"):
			result.section = "Lecture" + result.section[3:7]
			
	# END OF SUPER UGLY CODE#
	
	
	
	
	#Get all info for the class
	for result in usercoursedata:
		if result.rating !=0:
			rating+=result.rating
			ratingcount+=1
		
		if result.difficult !=0:
			difficult+=result.difficult-1
		
		if result.heavyworkload !=0:
			heavyworkload+=result.heavyworkload-1
		
		if result.gladtook !=0:
			gladtook+=result.gladtook-1
		
		if result.interesting !=0:
			interesting+=result.interesting-1
			extracount+=1
			
				
		
		
		if result.user == user.username:
			userRating = result.rating
			ratingid = result.id
			userDifficult = result.difficult
			userInteresting = result.interesting
			userGladtook = result.gladtook
			userHeavyworkload = result.heavyworkload
			
		
	if rating !=0:
		rating = rating/ratingcount
		
	if request.method == 'POST':
		form = RatingForm(request.POST)
		
		if form.is_valid():
			if ratingid!=0:
				if form.cleaned_data['rating']:
					ratingObject = userclasstable(courseidentifier=courseidentifier, user=user.username, rating = form.cleaned_data['rating'], difficult=userDifficult,
					interesting=userInteresting, gladtook=userGladtook, heavyworkload=userHeavyworkload, id=ratingid)
					ratingObject.save()
				elif form.cleaned_data['difficult']:
					ratingObject = userclasstable(courseidentifier=courseidentifier, user=user.username, rating = userRating, difficult = form.cleaned_data['difficult'],  id=ratingid,
					interesting = userInteresting, gladtook=userGladtook, heavyworkload=userHeavyworkload)
					ratingObject.save()
				elif form.cleaned_data['interesting']:
					ratingObject = userclasstable(courseidentifier=courseidentifier, user=user.username, rating = userRating, difficult = userDifficult,  id=ratingid,
					interesting = form.cleaned_data['interesting'], gladtook=userGladtook, heavyworkload=userHeavyworkload)
					ratingObject.save()
				elif form.cleaned_data['heavyworkload']:
					ratingObject = userclasstable(courseidentifier=courseidentifier, user=user.username, rating = userRating, difficult = userDifficult,  id=ratingid,
					interesting = userInteresting, gladtook=userGladtook, heavyworkload=form.cleaned_data['heavyworkload'])
					ratingObject.save()
				elif form.cleaned_data['gladtook']:
					ratingObject = userclasstable(courseidentifier=courseidentifier, user=user.username, rating = userRating, difficult = userDifficult,  id=ratingid,
					interesting = userInteresting, gladtook=form.cleaned_data['gladtook'], heavyworkload=userHeavyworkload)
					ratingObject.save()
					
			else:
				if form.cleaned_data['rating']:
					ratingObject = userclasstable(courseidentifier=courseidentifier, user=user.username, rating = form.cleaned_data['rating'], difficult=userDifficult,
					interesting=userInteresting, gladtook=userGladtook, heavyworkload=userHeavyworkload)
					ratingObject.save()
				elif form.cleaned_data['difficult']:
					ratingObject = userclasstable(courseidentifier=courseidentifier, user=user.username, rating = userRating, difficult = form.cleaned_data['difficult'],  id=ratingid,
					interesting = userInteresting, gladtook=userGladtook, heavyworkload=userHeavyworkload)
					ratingObject.save()
				elif form.cleaned_data['interesting']:
					ratingObject = userclasstable(courseidentifier=courseidentifier, user=user.username, rating = userRating, difficult = userDifficult,  id=ratingid,
					interesting = form.cleaned_data['interesting'], gladtook=userGladtook, heavyworkload=userHeavyworkload)
					ratingObject.save()
				elif form.cleaned_data['heavyworkload']:
					ratingObject = userclasstable(courseidentifier=courseidentifier, user=user.username, rating = userRating, difficult = userDifficult,  id=ratingid,
					interesting = userInteresting, gladtook=userGladtook, heavyworkload=form.cleaned_data['heavyworkload'])
					ratingObject.save()
				elif form.cleaned_data['gladtook']:
					ratingObject = userclasstable(courseidentifier=courseidentifier, user=user.username, rating = userRating, difficult = userDifficult,  id=ratingid,
					interesting = userInteresting, gladtook=form.cleaned_data['gladtook'], heavyworkload=userHeavyworkload)
					ratingObject.save()

			
			usercoursedata = userclasstable.objects.filter(courseidentifier=courseidentifier)
			ratingcount = 0
			rating = 0
			gladtook=0
			heavyworkload=0
			interesting=0
			difficult=0
			
			#Get all info for the class
			for result in usercoursedata:
				if result.rating !=0:
					rating+=result.rating
					ratingcount+=1
				
				if result.difficult !=0:
					difficult+=result.difficult-1
		
				if result.heavyworkload !=0:
					heavyworkload+=result.heavyworkload-1
		
				if result.gladtook !=0:
					gladtook+=result.gladtook-1
		
				if result.interesting !=0:
					interesting+=result.interesting-1
					
				
				if result.user == user.username:
					userRating = result.rating
					
			if rating !=0:
				rating = rating/ratingcount
			
			
	else:
		form = RatingForm()
		
	return render_to_response("classdisplay/classdisplay.html",{
		"classinfo":classinfo,
		"classtitle":classtitle,
		"usercoursedata":usercoursedata,
		"rating":rating,
		"ratingcount":ratingcount,
		"difficult":difficult,
		"heavyworkload":heavyworkload,
		"gladtook":gladtook,
		"interesting":interesting,
		"prof":prof,
		"extracount":extracount,
		"form":form,
		"user":user,
		"userRating":userRating,
		"spring09info":spring09info,
		"fall09info":fall09info,
		"winter09info":winter09info,
		
		
	})
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	